<?php
class Controller_Product_Categories extends Controller_Abstract
{
	function actionList()
	{
		$this->_view['title'] = '列表';
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$categories = Product_Categories::find('parent_id=?', $id)->asArray()->getAll();
		foreach ($categories as $k => $c)
		{
			$c['children_count'] = count(Product_Categories::find("parent_id = {$c['id']}")->getAll());
			$categories[$k]['children_count'] = $c['children_count'];
		}
		$this->_view['categories'] = $categories;
		$ancestor = Product_Categories::getAncestorById($id);
		$this->_view['ancestor'] = $ancestor;
	}
	
	function actionCreate()
	{
		$this->_view['title'] = '新建';
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		if ($id)
		{
			$parent = Product_Categories::find('id =?', $id)->asArray()->getOne();
		}
		else
		{
			$parent = array('id' => 0, 'name' => '——');
		}
		$this->_view['parent'] = $parent;
	}
	
	function actionCreateSave()
	{
		$this->_view['title'] = '新建（保存）';
		if ($this->_context->isPOST())
		{
			$name = trim($_POST['name']);
			$parent_id = $_POST['parent_id'];
			if ($name)
			{
				$categories = Product_Categories::create($parent_id, $name);
				if ($categories['ack'] == FAILURE)
				{
					return $this->_redirectMessage('请检查产品分类名/编号前缀', '请重新输入产品分类名称/编号前缀', url('Product::Categories/Create', array('id' => $parent_id)), $categories['ack']);
				}
				if ($categories['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('新建成功', '返回列表', url('Product::Categories/List', array('id' => $parent_id)), $categories['ack']);
				}
			}
			else
			{
				return $this->_redirectMessage('新建失败', '产品分类名称不能为空', url('Product::Categories/Create', array('id' => $parent_id)), FAILURE);
			}
		}
		return $this->_redirect(url('Product::Categories/List'));
	}
	
	function actionEdit()
	{
		$this->_view['title'] = '编辑';
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		if ($id)
		{
			$categories = Product_Categories::find('id=?', $id)->asArray()->getOne();
			$this->_view['categories'] = $categories;
		}
		else
		{
			return $this->_redirectMessage('错误', '该分类不存在', url('Product::Categories/List'), FAILURE);
		}
	}
	
	function actionEditSave()
	{
		$this->_view['title'] = '编辑（保存）';
		if ($this->_context->isPOST())
		{
			$id = $_POST['id'];
			$name =  trim($_POST['name']);
			$categories = Product_Categories::edit($id, $name);
			if ($categories['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('编辑成功', '返回列表', url('Product::Categories/List', array('id' => $_POST['parent_id'])), $categories['ack']);
			}
			else
			{
				return $this->_redirectMessage('编辑失败', '无效分类或名称已存在', url('Product::Categories/Edit', array('id' => $id)), $categories['ack']);
			}
			
		}
		else
		{
			return $this->_redirect(url('Product::Categories/list'));
		}
	}
	
	function actionDelete()
	{
		$this->_view['title'] = '删除';
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$parent_id = isset($_GET['parent_id']) ? $_GET['parent_id'] : 0;
		if ($id)
		{
			$result = Product_Categories::delete($id);
			if ($result['ack'] == FAILURE && $result['message'] == 'child categories is exists!')
			{
				return $this->_redirectMessage('删除失败', '该分类还有子类', url('Product::Categories/List', array('id' => $parent_id)), $result['ack']);
			}
			else
			{
				if ($result['ack'] == FAILURE && $result['message'] == 'product is exists!')
				{
					return $this->_redirectMessage('删除失败', '该子分类还有产品,不能删除', url('Product::Categories/List', array('id' => $parent_id)), $result['ack']);
				}
				if ($result['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('删除成功', '正在返回..', url('Product::Categories/List', array('id' => $parent_id)), $result['ack']);
				}
			}
		}
		else
		{
			return $this->_redirectMessage('删除失败', '该分类不存在', url('Product::Categories/List'), FAILURE);
		}
	}
}